package com.xd.cloud.service.device;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xd.cloud.common.util.DateUtil;
import com.xd.cloud.dao.Page;
import com.xd.cloud.dao.device.SimDao;
import com.xd.cloud.entity.device.Sim;
import com.xd.cloud.entity.device.SimPo;

/**   
 * 功能描述：sim信息Service
 * @author: wangshanshan   
 * @date: 2017年12月12日 下午2:08:42 
 */
@Service
public class SimService {

	@Autowired
	private SimDao simDao;

	public List<Sim> getAllSim(int pageNo, int pageSize) {
		Page dataPage = simDao.getAllSim(pageNo, pageSize);
		if(dataPage!=null) {
			List<Sim> retuList = new ArrayList<Sim>();
			List<Object[]> dataList = dataPage.getResult();
			if (dataList != null) {
				for (Object[] item:dataList){
					SimPo obj = new SimPo();
					obj.setId((Integer)item[0]);
					obj.setSim_card((String)item[1]);
					if(obj.getSim_card().length()>25) {
						obj.setSim_card_substr(obj.getSim_card().substring(0, 25)+"...");
					}else {
						obj.setSim_card_substr(obj.getSim_card());
					}
					obj.setRegistration_time((Date)item[2]);
					obj.setRegistration_time_str(DateUtil.DateToString(DateUtil.format_YYYY_MM_DD_HH_MM_SS, obj.getRegistration_time()));
					obj.setStatus((Integer)item[3]);
					obj.setStatus_name((obj.getStatus()==0?"未启用":"已启用"));
					obj.setInstall((Integer)item[4]);
					obj.setInstall_name(obj.getInstall()==0?"未安装":"已安装");
					obj.setDevice_id((String)item[5]);
					obj.setCompany_id((Integer)(item[6]==null?-1:item[6]));
					obj.setCompany_name((String)item[7]);
					obj.setVehid((String)(item[8]==null?"":item[8]));
					retuList.add(obj);
				}
				return retuList;
			}
		}
		return null;
	}
	
	public Sim getSimById(int id) {
		return simDao.getSimById(id);
	}
	
	public Sim getSimBySimCard(String sim_card) {
		return simDao.getSimBySimCard(sim_card);
	}
	
	public Sim getSimObjectById(int id){
		Object[] item = (Object[]) simDao.getSimObjectById(id);
		if(item!=null) {
			SimPo obj = new SimPo();
			obj.setId((Integer)item[0]);
			obj.setSim_card((String)item[1]);
			obj.setRegistration_time((Date)item[2]);
			obj.setStatus((Integer)item[3]);
			obj.setCompany_id((Integer)(item[4]==null?-1:item[4]));
			obj.setCompany_name((String)item[5]);
			obj.setRemark((String)item[6]);
			return obj;
		}
		return null;
	}
	
	public boolean save(Sim item){
		simDao.save(item);
		return true;
	}
	
	public boolean update(Sim item){
		simDao.update(item);
		return true;
	}
	
	public boolean deleteInfo(int id){
		Sim item = getSimById(id);
		if(item!=null) {
			simDao.delete(item);
			return true;
		}else{
			return false;
		}
	}
	
	public List<SimPo> getSimList() {
		List<Object[]> list = simDao.getSimList();
		if(list!=null && list.size()>0) {
			List<SimPo> retuList = new ArrayList<SimPo>();
			for(Object[] item : list) {
				if(item!=null) {
					SimPo obj = new SimPo();
					obj.setId((Integer)item[0]);
					obj.setSim_card((String)item[1]);
					obj.setCompany_id((Integer)(item[2]==null?-1:item[2]));
					obj.setCompany_name((String)item[3]);
					retuList.add(obj);
				}
			}
			return retuList;
		}
		return null;
	}
	
	public boolean updateSimInstall(int id, int install) {
		Sim item = getSimById(id);
		if(item!=null) {
			simDao.updateSimInstall(id, install);
			return true;
		}else{
			return false;
		}
	}
	
	public Sim getSim(String sim_card, int company_id) {
		return simDao.getSim(sim_card, company_id);
	}
	
	
	public Object getSimObjectByDeviceId(int device_id) {
		Object[] item = (Object[]) simDao.getSimObjectByDeviceId(device_id);
		if(item!=null) {
			SimPo obj = new SimPo();
			obj.setId((Integer)item[0]);
			obj.setSim_card((String)item[1]);
			return obj;
		}
		return null;
	}
}
